package vn.demand.dragon.model
{
	import mx.collections.XMLListCollection;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.business.LoadProjectItemsByReferenceDelegate;
	import vn.demand.dragon.model.business.SaveProjectItemsDelegate;
	
	public class ProjectItemProxy extends Proxy implements IProxy
	{
		public static const NAME:String = "ProjectItemProxy";
		
		public var errorStatus:String;
		
		private var pitems:XMLListCollection;
		
		public static function makeId(projectVO:Object):String {
        	return NAME + projectVO.project_id + "::";
        }
		
		public function ProjectItemProxy(proxyName:String, data:Object = null) 
        {
            super (proxyName, data );
        }
        
        public function get projectVO():Object {
        	return data as Object;
        }
        
        public function loadProjectItemByReference():void {
			var delegate:LoadProjectItemsByReferenceDelegate = new LoadProjectItemsByReferenceDelegate(resultLoadProjectItemByReference, failedLoadProjectItemByReference);
			delegate.loadProjectItemsByReference(projectVO.project_id);
		}
		
		public function resultLoadProjectItemByReference(rpcEvent:Object):void {
			pitems = new XMLListCollection(rpcEvent.result.pitems.item);
			sendNotification(FFIAApplicationFacade.LOAD_PITEMS_BY_REFERENCE_SUCCESS, proxyName, projectVO.project_id);
		}
		
		public function failedLoadProjectItemByReference(rpcEvent:Object):void {
			errorStatus = "Could not load reference lines!";
			sendNotification(FFIAApplicationFacade.LOAD_PITEMS_BY_REFERENCE_FAILED, errorStatus, projectVO.project_id);
		}
		
		public function get pitemsDP():XMLListCollection {
			return pitems;
		}
		
		public function newProjectItem():XML {
			var xml:XML = new XML("<item><pitem_project>" + projectVO.project_id  +   "</pitem_project></item>");
			return xml;
		}
		
		public function removeProjectItem(selIndex:int):Object {
			return pitemsDP.removeItemAt(selIndex);
		}
		
		public function copyProjectItem(projectVO:Object, copyItem:Object, targetRow:int):Object {
			var item:XML = new XML(XML(copyItem).toXMLString());
			item['pitem_project'] = String(projectVO.project_id);
			item['pitem_id'] = '';
			if (targetRow == pitemsDP.length - 1) {
				pitemsDP.addItemAt(item, targetRow);
			} else {
				pitemsDP.setItemAt(item, targetRow);
			}
			return item;
		}
		
		public function saveProjectItems():void {
			var xml:String = "<root>" + pitemsDP.toXMLString() + "</root>";
			var delegate:SaveProjectItemsDelegate = new SaveProjectItemsDelegate(resultSaveProjectItems, faultSaveProjectItems);
			delegate.saveProjectItems(xml);
		}
		
		public function resultSaveProjectItems(rcpEvent:Object):void {
			var success:Boolean = rcpEvent.result.success == 'true'; 
			if (success) {
				sendNotification(FFIAApplicationFacade.PROJECT_ITEM_UPDATED, projectVO, projectVO.project_id);
			} else {
				RIALogger.error(this, "Save quotation lines failed with error message: \n" + rcpEvent.result.errors);
				sendNotification(FFIAApplicationFacade.PROJECT_ITEM_UPDATE_FAILED, rcpEvent.result.errors, projectVO.project_id);				
			}
		}
		
		public function faultSaveProjectItems(rcpEvent:Object):void {
			errorStatus = "Something wrong when save quotation lines!";
			sendNotification(FFIAApplicationFacade.PROJECT_ITEM_UPDATE_FAILED, errorStatus, projectVO.project_id);
		}
	}
}